package com.pm.controller.log;

import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.log.TaskSubtasksCheckLog;
import com.pm.entity.po.security.User;
import com.pm.entity.po.task.TaskSubtasks;
import com.pm.mapper.security.UserMapper;
import com.pm.service.log.TaskSubtasksCheckLogService;
import com.pm.service.task.TaskSubtasksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "目标任务进度审核表")
@UserLoginToken
@RestController
@RequestMapping("/taskSubtasksCheckLog")
public class TaskSubtasksCheckLogController {
    @Autowired
    TaskSubtasksCheckLogService taskSubtasksCheckLogService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    private TaskSubtasksService distributionService;

    @ApiOperation("查目标任务进度审核表")
    @PostMapping("/getList")
    @ResponseBody
    @MyLog(value = "查目标任务进度审核表")
    public Response<List<TaskSubtasksCheckLog>> getList(TaskSubtasksCheckLog taskSubtasksCheckLog, @CurrentUser
            User user) {
        Integer id = user.getUserId();
        Integer taskId = userMapper.getTaskId(id);
        taskSubtasksCheckLog.setTaskId(taskId);
        List<TaskSubtasksCheckLog> result = taskSubtasksCheckLogService.getList(taskSubtasksCheckLog);
        Response<List<TaskSubtasksCheckLog>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("修改目标任务进度审核表读状态")
    @PostMapping("/updateReda")
    @ResponseBody
    @MyLog(value = "修改目标任务进度审核表读状态")
    public Response<Integer> updateReda(TaskSubtasksCheckLog taskSubtasksCheckLog) {
        int res = taskSubtasksCheckLogService.update(taskSubtasksCheckLog);
        if (res > 0) {
            return new Response<>(ExpCodeState.update_data_success);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }

    @ApiOperation("更新目标任务审核记录")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "更新目标任务审核记录")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(TaskSubtasksCheckLog taskSubtasksCheckLog) {
        int res = taskSubtasksCheckLogService.update(taskSubtasksCheckLog);
        int i = 1;
        if (taskSubtasksCheckLog.getStatus() == 1) {
            i = 2;
        }
        if (taskSubtasksCheckLog.getStatus() == -1) {
            i = -1;
        }
        TaskSubtasks s = new TaskSubtasks();
        switch (taskSubtasksCheckLog.getQuarter()) {
            case 1:
                s.setOneQuarterDegreeStatus(i);
                break;
            case 2:
                s.setTwoQuarterDegreeStatus(i);
                break;
            case 3:
                s.setThreeQuarterDegreeStatus(i);
                break;
            case 4:
                s.setAllDegreeStatus(i);
                break;
        }
        distributionService.update(s);
        if (res > 0) {
            return new Response<>(ExpCodeState.update_data_success, res);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }

}
